package org.apache.commons.graph.export

import kotlinx.io.Buffer
import kotlinx.io.readString
import org.apache.commons.graph.CommonsGraph.export
import org.apache.commons.graph.CommonsGraph.newUndirectedMutableGraph
import org.apache.commons.graph.builder.AbstractGraphConnection
import org.apache.commons.graph.model.BaseLabeledVertex
import org.apache.commons.graph.model.BaseLabeledWeightedEdge
import org.apache.commons.graph.model.UndirectedMutableGraph
import org.junit.After
import org.junit.Before
import org.junit.Test
import java.io.PrintStream

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

class ExportTestCase {
    private var actual: UndirectedMutableGraph<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>? = null

    @Before
    fun setUp() {
        actual =
            newUndirectedMutableGraph(object :
                AbstractGraphConnection<BaseLabeledVertex, BaseLabeledWeightedEdge<Double>>() {
                override fun connect() {
                    val start = addVertex(BaseLabeledVertex("start"))
                    val a = addVertex(BaseLabeledVertex("a"))
                    val b = addVertex(BaseLabeledVertex("b"))
                    val goal = addVertex(BaseLabeledVertex("goal"))

                    addEdge(BaseLabeledWeightedEdge<Double>("start <-> a", 1.5)).from(start).to(a)
                    addEdge(BaseLabeledWeightedEdge<Double>("a <-> b", 2.0)).from(a).to(b)
                    addEdge(BaseLabeledWeightedEdge<Double>("a <-> goal", 2.0)).from(a).to(goal)
                    addEdge(BaseLabeledWeightedEdge<Double>("b <-> goal", 2.0)).from(b).to(goal)
                }
            })
    }

    @After
    fun tearDown() {
        actual = null
    }

    @Test
    @Throws(Exception::class)
    fun testShouldPrintDotFormat() {
        export(actual!!).withName("DotFormatGraph")
            .usingDotNotation()
            .withVertexLabels(VertexLabelMapper())
            .withEdgeWeights(EdgeWeightMapper())
            .withEdgeLabels(EdgeLabelMapper())
            .to(System.out)
    }

    @Test
    @Throws(Exception::class)
    fun testShouldPrintGraphML() {
        export(actual!!).withName("GraphMLGraph")
            .usingGraphMLFormat()
            .withVertexLabels(VertexLabelMapper())
            .withEdgeWeights(EdgeWeightMapper())
            .withEdgeLabels(EdgeLabelMapper())
            .to(System.out)
    }

    @Test
    @Throws(Exception::class)
    fun testShouldPrintGraphMLFormat() {
        export(actual!!).usingGraphMLFormat().to(System.out)
    }
}

fun<V, E, T : AbstractExporter<V, E, T>> AbstractExporter<V,E,T>.to(out: PrintStream){
    val buffer = Buffer()
    to(buffer)
    out.print(buffer.readString())
}